package com.zy.service.bs.impl;

import com.zy.dao.bs.*;
import com.zy.dao.crm.CustomerDao;
import com.zy.dao.crm.CustomerInfoDao;
import com.zy.dao.sys.UserDao;
import com.zy.dao.sys.UserRoleDao;
import com.zy.entity.bs.*;
import com.zy.entity.crm.Customer;
import com.zy.entity.crm.CustomerInfo;
import com.zy.entity.enums.bs.CheckStatusEnum;
import com.zy.entity.sys.User;
import com.zy.entity.sys.UserRole;
import com.zy.pojo.constants.Constant;
import com.zy.pojo.exceptions.ExceptionEnum;
import com.zy.pojo.exceptions.ServiceException;
import com.zy.utils.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zy.entity.enums.StatusEnum;
import com.zy.pojo.query.bs.CustomerClubApplyQuery;
import com.zy.pojo.vos.common.PageVo;
import com.zy.service.base.impl.BaseServiceImpl;
import com.zy.service.bs.CustomerClubApplyService;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Objects;

@Service("customerClubApplyService")
public class CustomerClubApplyServiceImpl extends BaseServiceImpl<CustomerClubApply> implements CustomerClubApplyService {

	@Autowired
	private CustomerClubApplyDao customerClubApplyDao;
	@Autowired
	private UserDao userDao;
	@Autowired
	private CustomerClubDao customerClubDao;
	@Autowired
	private CustomerDao customerDao;
	@Autowired
	private UserRoleDao userRoleDao;
	@Autowired
	private MsgDao msgDao;
	@Autowired
	private ClubDao clubDao;
	@Autowired
	private CarDao carDao;
	@Autowired
	private CustomerInfoDao customerInfoDao;

	@Override
	public PageVo pageCustomerClubApply(CustomerClubApplyQuery customerClubApplyQuery, Integer start, Integer limit) {
		return new PageVo(start, limit, this.customerClubApplyDao.countCustomerClubApply(customerClubApplyQuery), this.customerClubApplyDao.listCustomerClubApply(customerClubApplyQuery, start, limit));
	}

	@Override
	public void saveCustomerClubApply(CustomerClubApply customerClubApply) {
		customerClubApply.setApplyTime(DateUtil.getTime());
		this.customerClubApplyDao.save(customerClubApply);
	}
	
	@Override
	public void updateCustomerClubApply(CustomerClubApply customerClubApply) {
		this.customerClubApplyDao.updateByPrimaryKeySelective(customerClubApply);
	}

	@Override
	public void removeCustomerClubApply(long id) {
		try {
			this.customerClubApplyDao.removeByPrimaryKey(id);
		} catch (Exception e) {
			log.info(e.getMessage(),e);
			CustomerClubApply customerClubApply = new CustomerClubApply();
			this.customerClubApplyDao.updateByPrimaryKeySelective(customerClubApply);
		}
	}

	@Override
	public void recover(long id) {
		CustomerClubApply customerClubApply = new CustomerClubApply();
		customerClubApply.setId(id);
		this.customerClubApplyDao.updateByPrimaryKeySelective(customerClubApply);
	}

	@Override
	public void checkCustomer(CustomerClubApply customerClubApply) {
		/**
		 * 审核通过给予会员该俱乐部的成员权限 并创建用户 会员绑定俱乐部  如果该申请人是游客则先创建会员
		 */
		StringBuffer msgs=new StringBuffer();
		CustomerClubApply selectCub = customerClubApplyDao.getByPrimaryKey(customerClubApply.getId());
		if(!Objects.equals(selectCub.getCheckStatus(),CheckStatusEnum.WAIT.getValue())){
			throw new ServiceException("该申请已操作请不要重复操作");
		}
		Long customerId = customerClubApply.getCustomerId();
		if(Objects.equals(customerClubApply.getCheckStatus(), CheckStatusEnum.YES.getValue())){
            Club club = new Club();
            BeanUtils.copyProperties(selectCub,club,"id");
			Customer customer = customerDao.getByPrimaryKey(customerId);
			CustomerClub customerClub =  new CustomerClub();
            club.setUserId(customer.getUserId());
			if(Objects.isNull(customer.getUserId())){
				User user = new User();
				String salt = CodeUtil.getUniqueCode();
				user.setSalt(salt);
				user.setUserName(customer.getMobile());
				user.setSalt(salt);
				user.setUserPwd(MD5Util.getMD5Pwd(salt, PropertiesUtil.getConfigProperty("user.resetPwd")));
			    userDao.save(user);
                club.setUserId(user.getId());

			}
			Example ex = new Example(CustomerInfo.class);
			ex.createCriteria().andEqualTo("customerId",customerId);
			CustomerInfo cu =  customerInfoDao.getByExample(ex);
			if(Objects.isNull(cu)){
                 cu = new CustomerInfo();
                 BeanUtils.copyProperties(customerClubApply,cu,"id");
                 customerInfoDao.save(cu);
			}
            Car car = new Car();
			BeanUtils.copyProperties(selectCub,car,"id");
			car.setCustomerId(customerId);
			car.setCreateUserId(customer.getUserId());
			car.setModifyUserId(customer.getUserId());
			carDao.save(car);
			club.setIsSet(0);
            clubDao.save(club);
            customerClub.setClubId(club.getId());
            customerClub.setCustomerId(customer.getId());
            customerClubDao.save(customerClub);
			UserRole userRole = new UserRole();
			BeanUtils.copyProperties(selectCub,userRole);
			userRole.setUserId(club.getUserId());
			userRole.setRoleId(Constant.MANAGER_ROLE_ID);
			userRoleDao.save(userRole);
			msgs.append("俱乐部创建审核通过");
		}else{
            msgs.append("俱乐部创建审核不通过");
        }

		Msg msg = new Msg();
		msg.setContent(msgs.toString());
		msg.setSendCustomerId(1L);
		msg.setIsRead(0);
		msg.setSendTime(DateUtil.getTime());
		msg.setIsReceive(0);
		msg.setHandlerCustomerId(customerId);
		msgDao.save(msg);
		customerClubApplyDao.updateByPrimaryKeySelective(selectCub);
	}

	@Override
	public List<CustomerClubApply> getCustomerClubApply(Long customerId) {
		return customerClubApplyDao.getCurrentClubApplyForCustomer(customerId);
	}

	@Override
	public void regiestCustomerCulb(CustomerClubApply customerClubApply) {
		customerClubApply.setApplyTime(DateUtil.getTime());
		customerClubApply.setCheckStatus(CheckStatusEnum.WAIT.getValue());
		this.customerClubApplyDao.save(customerClubApply);

	}

}
